package com.wyy.servlet.web;

import com.wyy.bean.Users;
import com.wyy.dao.impl.UsersDaoImpl;
import com.wyy.service.UsersService;
import com.wyy.service.impl.UsersServiceImpl;
import com.wyy.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @ClassName UsersServlet
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/26 21:45
 * @Version 1.0
 **/
@WebServlet(urlPatterns = "/power/user/users")
public class UsersServlet extends HttpServlet {
    
    private UsersService usersService = new UsersServiceImpl();
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("insert".equals(method)){
            insert(req, resp);
        }else{
            select(req,resp);
        }

    
    }
    
    //查询数据
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数（分页参数、模糊查询参数
        String index = req.getParameter("index");
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调用service  查询数据列表 查询总条数
        PageUtil pageUtil = new PageUtil();
        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setTotal(total);
        pageUtil.setList(usersList);
        //3.存值转发页面
        req.setAttribute("pu",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
        
    }

    //插入数据
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取参数
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        //调用service
        Users users = new Users();
        users.setLoginName(loginName);
        users.setPassword(password);
        users.setRealName(realName);
        users.setSex(Integer.parseInt(sex));
        users.setEmail(email);
        users.setAddress(address);
        users.setPhone(phone);
        users.setCardId(cardId);
        users.setDesc(desc);
        users.setRoleId(Integer.parseInt(roleId));
        int i = usersService.insertUser(users);
        //跳转页面
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功！');location.href = '/power/user/users?method=selected'</script>");
        }else if (i==-1){
            writer.println("<script>alert('新增失败-->该用户已存在！');location.href='/power/user/users?method=selected'</script>");
        }
        else{
            writer.println("<script>alert('新增失败');location.href='/power/user/users?method=selected'</script>");
        }
        
    }
}
